package neptune.manager.builder;

import neptune.manager.ExperimentException;
import neptune.manager.model.experiment.Experiment;
import neptune.manager.model.topology.IPAddress;
import neptune.manager.model.topology.MACAddress;
import neptune.manager.model.topology.NetworkInterface;
import neptune.manager.model.topology.Node;
import neptune.manager.model.topology.Topology;
import neptune.manager.storage.ExperimentRepositoryException;
import neptune.manager.storage.ExperimentsRepository;
import neptune.manager.storage.StorageReadException;
import neptune.manager.storage.StorageSaveException;
import neptune.manager.storage.TopologyDefinitionException;
import neptune.manager.storage.TopologyStorage;
import neptune.manager.storage.TopologyStorageFactory;
import neptune.manager.utils.IllegalValueException;
import neptune.manager.utils.Logger;

public class ControlIPAddrBuilderTEST {

	/**
	 * @param args
	 * @throws IllegalValueException
	 * @throws ControlAddressException
	 * @throws TopologyDefinitionException
	 * @throws StorageReadException
	 * @throws ExperimentRepositoryException
	 */
	public static void main(String[] args) throws IllegalValueException,
			ExperimentRepositoryException, StorageReadException,
			TopologyDefinitionException, ControlAddressException {
		IPAddressRange range = new IPAddressRange();
		range.addRange(new IPAddress(4, "192.168.0.1", "255.255.255.0", true),
				new IPAddress(4, "192.168.0.1", "255.255.255.0", true));
		range.addRange(new IPAddress(4, "10.0.0.254", "255.255.255.0", true),
				new IPAddress(4, "10.0.1.200", "255.255.255.0", true));

		ExperimentsRepository expRepo = new ExperimentsRepository() {

			@Override
			public void updateExperiment(Experiment experiment)
					throws ExperimentRepositoryException {

			}

			@Override
			public void setExperimentStopTime(String expId, long timestamp)
					throws ExperimentRepositoryException {

			}

			@Override
			public void setExperimentState(String expName, String expState)
					throws ExperimentRepositoryException {

			}

			@Override
			public void setExperimentStartTime(String expId, long timestamp)
					throws ExperimentRepositoryException {

			}

			@Override
			public String[] getExperiments()
					throws ExperimentRepositoryException {
				return new String[] { "exp1" };
			}

			@Override
			public String[][] getExperimentUsers(String exp)
					throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public String[][] getExperimentTable(int begin, int end,
					int sortId, boolean ascending, String username,
					boolean isAdmin) throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public long getExperimentStopTime(String expId)
					throws ExperimentRepositoryException {
				return 0;
			}

			@Override
			public String getExperimentState(String expName)
					throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public long getExperimentStartTime(String expId)
					throws ExperimentRepositoryException {
				return 0;
			}

			@Override
			public int getExperimentNumberForUser(String username,
					boolean isSystemAdmin) throws ExperimentRepositoryException {
				return 0;
			}

			@Override
			public Experiment getExperimentByName(String name)
					throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public String getExperimentAdmin(String expName)
					throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public String[] getActiveExperiments()
					throws ExperimentRepositoryException {
				return null;
			}

			@Override
			public void deleteExperimentUser(String exp, String usr)
					throws ExperimentRepositoryException {

			}

			@Override
			public boolean deleteExperiment(String expName)
					throws ExperimentRepositoryException {
				return true;

			}

			@Override
			public void deleteExperiment(Experiment experiment)
					throws ExperimentRepositoryException {

			}

			@Override
			public void createExperiment(String expName, String expAdmin,
					long creationTimestamp)
					throws ExperimentRepositoryException {

			}

			@Override
			public void addExperimentUser(String exp, String usr, String role)
					throws ExperimentRepositoryException {

			}

			@Override
			public void addExperimentToRepository(Experiment experiment)
					throws ExperimentException, ExperimentRepositoryException {

			}
		};

		TopologyStorageFactory topologyFactory = new TopologyStorageFactory() {

			@Override
			public TopologyStorage getTopologyStorage(String topologyName) {
				return new TopologyStorage() {

					@Override
					public void saveTopology(Topology topology)
							throws StorageSaveException {

					}

					@Override
					public Topology getTopology() throws StorageReadException,
							TopologyDefinitionException {
						try {
							Topology topo = new Topology();

							Node node = new Node(null, "node", "nomeNodo");
							NetworkInterface netIf = new NetworkInterface(
									"eth0", null, new MACAddress(
											MACAddressBuilder
													.generateRandomMAC()));

							netIf.addIpAddress(new IPAddress(4, "192.168.0.1",
									"255.255.255.0", true));

							NetworkInterface netIf2 = new NetworkInterface(
									"eth1", null, new MACAddress(
											MACAddressBuilder
													.generateRandomMAC()));

							netIf2.addIpAddress(new IPAddress(4, "10.0.0.254",
									"255.255.255.0", true));

							node.addNetworkInterface(netIf);
							node.addNetworkInterface(netIf2);
							topo.addNode(node);

							return topo;
						} catch (Exception e) {
							e.printStackTrace();
						}

						return null;
					}

					@Override
					public boolean deleteTopology() {
						return true;

					}

					@Override
					public void deleteTempTopology() {

					}

					@Override
					public Topology getTempTopology()
							throws StorageReadException,
							TopologyDefinitionException {
						return null;
					}

					@Override
					public void saveTempTopology(Topology topology)
							throws StorageSaveException {

					}
				};
			}
		};

		IPAddress addr = ControlIPAddressBuilder.buildControlIPAddress(range,
				topologyFactory.getTopologyStorage(null).getTopology(),
				expRepo, topologyFactory, Logger.createLogContext(""));

		System.out.println(addr);
	}
}
